package org.jgs1904.elasticsearchapi.index;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;

import java.io.IOException;

/**
 * @author 墨苍璃
 * @date 2023年06月04日22时00分
 * <p>
 * 创建索引
 */
public class CreateIndex {

    public static void main(String[] args) throws IOException {
        // 创建客户端对象
        // Create the low-level client
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        // 使用Jackson映射器创建传输层
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        // 创建API客户端
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 创建索引
        CreateIndexResponse createIndexResponse = client.indices().create(c -> c.index("user_test"));
        // 响应状态
        boolean acknowledged = createIndexResponse.acknowledged();
        System.out.println("索引操作 响应状态 = " + acknowledged);

        // 关闭ES客户端
        transport.close();
        restClient.close();

    }


//    @Test
//    public void test() throws IOException {
//
//        // 创建客户端对象
//        // Create the low-level client
//        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
//        // 使用Jackson映射器创建传输层
//        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
//        // 创建API客户端
//        ElasticsearchClient client = new ElasticsearchClient(transport);
//
//        // 创建索引
//        CreateIndexResponse createIndexResponse = client.indices().create(c -> c.index("user_test1"));
//        // 响应状态
//        boolean acknowledged = createIndexResponse.acknowledged();
//        System.out.println("索引操作 响应状态 = " + acknowledged);
//
//        // 关闭ES客户端
//        transport.close();
//        restClient.close();
//
//
//    }


}
